import os
import sys
import requests
import json

# 添加项目根目录到Python路径，以便导入config_reader模块
current_dir = os.path.dirname(os.path.abspath(__file__))
project_root = os.path.dirname(os.path.dirname(current_dir))
sys.path.append(project_root)

from config_reader import get_openai_hk_api_key

# 从配置文件获取API密钥
api_key = get_openai_hk_api_key()

# 香港OpenAI API配置
url = "https://api.openai-hk.com/v1/chat/completions"
headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {api_key}"
}

# 聊天请求数据
data = {
    "max_tokens": 1200,
    "model": "gpt-3.5-turbo",
    "temperature": 0.8,
    "top_p": 1,
    "presence_penalty": 1,
    "messages": [
        {
            "role": "system",
            "content": "You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible."
        },
        {
            "role": "user",
            "content": "你是chatGPT多少？"
        }
    ]
}

# 发送请求
response = requests.post(url, headers=headers, data=json.dumps(data).encode('utf-8'))
result = response.content.decode("utf-8")

print(result)